#include "udp_server.hpp"
#include <memory>

std::string toUpper(std::string request)
{
    std::string response;
    for (char ch : request)
        response += toupper(ch);
    return response;
}

bool check(std::string cmd)
{
    if(cmd.find("rm") != std::string::npos)
        return false;
    else if(cmd.find("kill") != std::string::npos)
        return false;
    else if(cmd.find("mv") != std::string::npos)
        return false;
    else if(cmd.find("while") != std::string::npos)
        return false;
    return true;
}

std::string exeCommand(std::string cmd)
{
    if(!check(cmd))
        return "You are bad!";

    FILE *fp = popen(cmd.c_str(), "r");
    if(nullptr == fp)
        return "Error";
    char line[1024];
    std::string res;
    while(fgets(line, sizeof(line), fp) != nullptr)
        res += line;
    pclose(fp);
    return res;
}

int main(int argc, char* argv[])
{
    if(argc != 2)
    {
        std::cout << "Wrong use\n";
        std::cout << "Usage: ./udp_server port\n";
        exit(-2);
    }
    uint16_t server_port = atoi(argv[1]);
    std::unique_ptr<mynet::UdpServer> usvr(new mynet::UdpServer(exeCommand, server_port));
    usvr->init_server();
    usvr->start_server();
    return 0;
}